package com.xwdeshj.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xwdeshj.dal.Dal_XwfarmBasicinfo;
import com.xwdeshj.entity.XwBusinessInfo;
import com.xwdeshj.entity.XwFarmBasicinfo;
import com.xwdeshj.entity.XwUserinfo;
import com.xwdeshj.repository.XwBusinessInfoRepository;
import com.xwdeshj.repository.XwFarmBasicinfoRepository;
import com.xwdeshj.repository.XwUserinfoRepository;
import com.xwdeshj.service.XwFarmBasicinfoService;
import com.xwdeshj.utils.helper;
import lombok.extern.slf4j.Slf4j;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by BKWJA on 2018/12/24.
 * 农家乐店铺基本信息实现层
 */
@Slf4j
@Service("xwFarmBasicinfoService")
public class XwFarmBasicinfoServiceImpl implements XwFarmBasicinfoService {

    @Autowired
    private XwFarmBasicinfoRepository xwFarmBasicinfoRepository;
    @Autowired
    private XwBusinessInfoRepository xwBusinessInfoRepository;
    @Autowired
    private XwUserinfoRepository xwUserinfoRepository;
    @Autowired
    private JdbcTemplate jdbcTemplate;
     @Autowired
    private Sid sid;

    Dal_XwfarmBasicinfo dal_xwfarmBasicinfo = new Dal_XwfarmBasicinfo();

    /**
     * 农家乐信息列表查询
     */
    @Override
    public JSONObject getfarminfolist(Integer pagesize, Integer pageindex, String starttime, String endtime,
                                      String searchval, String searchkey, String sortfild, String sortway,
                                      String type,String status, HttpServletRequest request) {
        JSONObject result = new JSONObject();

        result = dal_xwfarmBasicinfo.getfarminfolist(pagesize,pageindex,starttime,endtime,searchval,searchkey,
                    sortfild,sortway,type,status,jdbcTemplate);

        System.out.println("getfarminfolist:"+result.toString());
        return result;
    }

    /**
     *  商家详情——获取农家乐基本信息（一条）
     */
    @Override
    public JSONObject getfarminfo(String business_encryptId, String farm_encryptId, HttpServletRequest request) {
        JSONObject result = new JSONObject();

        if(!"".equals(business_encryptId) && !"".equals(farm_encryptId)){
            result = dal_xwfarmBasicinfo.getfarminfo(business_encryptId,farm_encryptId,jdbcTemplate);
        }else {
            result.put("status",40002);
            result.put("msg","business_encryptId,farm_encryptId不能为空！");
        }
        System.out.println("getfarminfo:"+result.toString());
        return result;
    }

    /**
     * 修改商家的游玩类型备注
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    public JSONObject modifyfarmplayRemarks(String farm_encryptId, String farmplay_encryptId,
                                            String farmplaymark,HttpServletRequest request) {
        JSONObject result = new JSONObject();
        String uid_encryptid = helper.ReadCookieMap(request,"uid_encryptid").toString();
        String uid = helper.ReadCookieMap(request,"uid").toString();
        if(!"".equals(uid_encryptid) && !"".equals(uid)){
            if(!"".equals(farm_encryptId)){

                XwFarmBasicinfo xwFarmBasicinfo = new XwFarmBasicinfo();
                xwFarmBasicinfo.setFarmEncryptid(farm_encryptId);
                boolean temp = xwFarmBasicinfoRepository.exists(Example.of(xwFarmBasicinfo));
                if(temp){
                    try {
                        XwFarmBasicinfo farmBasicinfo = xwFarmBasicinfoRepository.findByFarmEncryptid(farm_encryptId);
                        farmBasicinfo.setFarmPalytypeDesc(farmplaymark);
                        farmBasicinfo.setFarmPlaytypeEncryptid(farmplay_encryptId);
                        farmBasicinfo.setFarmEncryptid(farm_encryptId);
                        farmBasicinfo.setFarmUpdatetime(new Date());
                        xwFarmBasicinfoRepository.save(farmBasicinfo);
                        result.put("status",0);
                        result.put("msg","ok");
                    }catch (Exception e){
                        e.printStackTrace();
                        result.put("status",40002);
                        result.put("msg",e);
                    }

                }else {
                    result.put("status",40002);
                    result.put("msg","该店铺不存在！");
                }

            }else {
                result.put("status",40002);
                result.put("msg","farm_encryptId不能为空！");
            }
        }else {
            result.put("status",40001);
            result.put("msg","uid不能为空,请先登录！");
        }
        System.out.println("modifyfarmplayRemarks:"+result.toString());
        return result;
    }

    /**
     * 农家乐——更改店铺基本信息
     */
    @Override
    @Modifying
    @Transactional(rollbackFor = Exception.class)
    public JSONObject updatefarminfo(String business_encryptId, String farm_encryptId, String updatefiled,
                                     String updateval,HttpServletRequest request) {
        JSONObject result = new JSONObject();
        String uid_encryptid = helper.ReadCookieMap(request,"uid_encryptid").toString();
        String uid = helper.ReadCookieMap(request,"uid").toString();
        if(!"".equals(uid_encryptid) && !"".equals(uid)){
            if(!"".equals(farm_encryptId)){

                //验证农家信息是否存在
                XwFarmBasicinfo xwFarmBasicinfo = new XwFarmBasicinfo();
                xwFarmBasicinfo.setFarmEncryptid(farm_encryptId);
                boolean temp = xwFarmBasicinfoRepository.exists(Example.of(xwFarmBasicinfo));
                //验证商家信息是否存在
                XwBusinessInfo xwBusinessInfo = new XwBusinessInfo();
                xwBusinessInfo.setBusinessEncryptid(business_encryptId);
                xwBusinessInfo.setFarmEncryptid(farm_encryptId);
                boolean businsstemp = xwBusinessInfoRepository.exists(Example.of(xwBusinessInfo));
                if(temp && businsstemp){
                    try {
                        if(updatefiled.equals("tel")){    //修改电话
                            //第一步 ：应查询该电话号码是否 已经存在（1、用户信息表）
                            List<XwUserinfo> user = xwUserinfoRepository.findByUserTel(updateval.trim());
                            if(user.size() == 0){
                                //第二步：不存在则修改成功，反之，提示：您要修改的电话已经有人注册，请重新添加电话
                                XwBusinessInfo businessInfo = xwBusinessInfoRepository.findByBusinessEncryptidAndFarmEncryptid(business_encryptId,farm_encryptId);
                                businessInfo.setBusinessTel(updateval);
                                businessInfo.setBusinessUpdatetime(new Date());
                                xwBusinessInfoRepository.save(businessInfo);

                                //第三步：不存在的情况下，在修改商家的电话号码同时修改用户信息表改商家的电话号码
                                XwUserinfo userinfo = new XwUserinfo();
                                userinfo.setUserEncryptid(business_encryptId);
                                boolean userTeltemp = xwUserinfoRepository.exists(Example.of(userinfo));
                                if(userTeltemp){
                                    XwUserinfo xwUserinfo = xwUserinfoRepository.findByUserEncryptid(business_encryptId);
                                    xwUserinfo.setUserTel(updateval);
                                    xwUserinfoRepository.save(xwUserinfo);
                                }

                                result.put("status",0);
                                result.put("msg","ok");
                            }else {
                                //已被人注册情况下
                                result.put("status",40002);
                                result.put("msg","联系电话已被他人注册！");
                            }

                        }else if(updatefiled.equals("city")){ //修改城市
                            //店铺信息
                            XwFarmBasicinfo farmBasicinfo = xwFarmBasicinfoRepository.findByFarmEncryptid(farm_encryptId);
                            farmBasicinfo.setFarmCity(updateval);
                            farmBasicinfo.setFarmUpdatetime(new Date());
                            xwFarmBasicinfoRepository.save(farmBasicinfo);
                            result.put("status",0);
                            result.put("msg","ok");

                        }else if(updatefiled.equals("farm_district")){ //修改区县
                            //店铺信息
                            XwFarmBasicinfo farmBasicinfo = xwFarmBasicinfoRepository.findByFarmEncryptid(farm_encryptId);
                            farmBasicinfo.setFarmDistrict(updateval);
                            farmBasicinfo.setFarmUpdatetime(new Date());
                            xwFarmBasicinfoRepository.save(farmBasicinfo);
                            result.put("status",0);
                            result.put("msg","ok");

                        }else if(updatefiled.equals("area")){ //经营面积
                            //店铺信息
                            XwFarmBasicinfo farmBasicinfo = xwFarmBasicinfoRepository.findByFarmEncryptid(farm_encryptId);
                            farmBasicinfo.setFarmBusinessArea(updateval);
                            farmBasicinfo.setFarmUpdatetime(new Date());
                            xwFarmBasicinfoRepository.save(farmBasicinfo);
                            result.put("status",0);
                            result.put("msg","ok");

                        }else if(updatefiled.equals("farm_status")){ //经营状态
                            //店铺信息
                            XwFarmBasicinfo farmBasicinfo = xwFarmBasicinfoRepository.findByFarmEncryptid(farm_encryptId);
                            farmBasicinfo.setFarmStatus(Integer.parseInt(updateval));
                            farmBasicinfo.setFarmUpdatetime(new Date());
                            xwFarmBasicinfoRepository.save(farmBasicinfo);
                            result.put("status",0);
                            result.put("msg","ok");

                        }else if(updatefiled.equals("farm_address")){ //住所
                            //店铺信息
                            XwFarmBasicinfo farmBasicinfo = xwFarmBasicinfoRepository.findByFarmEncryptid(farm_encryptId);
                            farmBasicinfo.setFarmAddress(updateval);
                            farmBasicinfo.setFarmUpdatetime(new Date());
                            xwFarmBasicinfoRepository.save(farmBasicinfo);
                            result.put("status",0);
                            result.put("msg","ok");

                        }else if(updatefiled.equals("farm_dining")){ //用餐接待
                            //店铺信息
                            XwFarmBasicinfo farmBasicinfo = xwFarmBasicinfoRepository.findByFarmEncryptid(farm_encryptId);
                            farmBasicinfo.setFarmDiningReception(updateval);
                            farmBasicinfo.setFarmUpdatetime(new Date());
                            xwFarmBasicinfoRepository.save(farmBasicinfo);
                            result.put("status",0);
                            result.put("msg","ok");

                        }else if(updatefiled.equals("farm_scale")){ //商家规模
                            //店铺信息
                            XwFarmBasicinfo farmBasicinfo = xwFarmBasicinfoRepository.findByFarmEncryptid(farm_encryptId);
                            farmBasicinfo.setFarmScale(updateval);
                            farmBasicinfo.setFarmUpdatetime(new Date());
                            xwFarmBasicinfoRepository.save(farmBasicinfo);
                            result.put("status",0);
                            result.put("msg","ok");

                        }else if(updatefiled.equals("farm_consume")){ //人均消费
                            //店铺信息
                            XwFarmBasicinfo farmBasicinfo = xwFarmBasicinfoRepository.findByFarmEncryptid(farm_encryptId);
                            farmBasicinfo.setFarmConsume(Double.parseDouble(updateval));
                            farmBasicinfo.setFarmUpdatetime(new Date());
                            xwFarmBasicinfoRepository.save(farmBasicinfo);
                            result.put("status",0);
                            result.put("msg","ok");

                        }else if(updatefiled.equals("farm_room")){ //住宿容纳
                            //店铺信息
                            XwFarmBasicinfo farmBasicinfo = xwFarmBasicinfoRepository.findByFarmEncryptid(farm_encryptId);
                            farmBasicinfo.setFarmRoom(updateval);
                            farmBasicinfo.setFarmUpdatetime(new Date());
                            xwFarmBasicinfoRepository.save(farmBasicinfo);
                            result.put("status",0);
                            result.put("msg","ok");

                        }else if(updatefiled.equals("bus_route")){ //路线情况（公交路线）
                            //店铺信息
                            XwFarmBasicinfo farmBasicinfo = xwFarmBasicinfoRepository.findByFarmEncryptid(farm_encryptId);
                            farmBasicinfo.setFarmBusRoute(updateval);
                            farmBasicinfo.setFarmUpdatetime(new Date());
                            xwFarmBasicinfoRepository.save(farmBasicinfo);
                            result.put("status",0);
                            result.put("msg","ok");

                        }else if(updatefiled.equals("self_route")){ //路线情况（自驾路线）
                            //店铺信息
                            XwFarmBasicinfo farmBasicinfo = xwFarmBasicinfoRepository.findByFarmEncryptid(farm_encryptId);
                            farmBasicinfo.setFarmSelfRoute(updateval);
                            farmBasicinfo.setFarmUpdatetime(new Date());
                            xwFarmBasicinfoRepository.save(farmBasicinfo);
                            result.put("status",0);
                            result.put("msg","ok");

                        }else if(updatefiled.equals("farm_Intr_info")){ //商家介绍
                            //店铺信息
                            XwFarmBasicinfo farmBasicinfo = xwFarmBasicinfoRepository.findByFarmEncryptid(farm_encryptId);
                            farmBasicinfo.setFarmIntrInfo(updateval);
                            farmBasicinfo.setFarmUpdatetime(new Date());
                            xwFarmBasicinfoRepository.save(farmBasicinfo);
                            result.put("status",0);
                            result.put("msg","ok");
                        }

                    }catch (Exception e){
                        e.printStackTrace();
                        result.put("status",40002);
                        result.put("msg",e);
                    }

                }else {
                    result.put("status",40002);
                    result.put("msg","该店铺不存在！");
                }

            }else {
                result.put("status",40002);
                result.put("msg","farm_encryptId不能为空！");
            }
        }else {
            result.put("status",40001);
            result.put("msg","uid不能为空,请先登录！");
        }
        System.out.println("updatefarminfo:"+result.toString());
        return result;
    }



}
